<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="common/include :: header('消费明细服务清单')" />
</head>
<body>
<div class="ui-layout-center">
    <div class="col-sm-12 search-collapse">
        <div class="col-sm-12" >
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-6 control-label">客户姓名：</label>
                    <label class="col-sm-6 control-label">[[${statement.customerName}]]</label>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-6 control-label">联系方式：</label>
                    <label class="col-sm-6 control-label">[[${statement.customerPhone}]]</label>
                </div>
            </div>
        </div>
        <div class="col-sm-12" >

            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-6 control-label">车牌号码：</label>
                    <label class="col-sm-6 control-label">[[${statement.licensePlate}]]</label>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-6 control-label">汽车类型：</label>
                    <label class="col-sm-6 control-label">[[${statement.carSeries}]]</label>
                </div>
            </div>
        </div>
        <div class="col-sm-12" >
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-6 control-label">服务类型：</label>
                    <label class="col-sm-6 control-label">[[${@dict.getLabel('si_service_catalog',statement.serviceType)}]]</label>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-6 control-label">到店时间：</label>
                    <label class="col-sm-6 control-label" th:text="${#dates.format(statement.actualArrivalTime, 'yyyy-MM-dd HH:mm')}"></label>
                </div>
            </div>
        </div>
        <div class="col-sm-12" >
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-6 control-label">总消费金额：</label>
                    <label class="col-sm-6 control-label" id="totalAmount">[[${statement.totalAmount}]]</label>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-6 control-label">实付价格：</label>
                    <label class="col-sm-6 control-label" id="actuallyPaid">[[${statement.totalAmount-statement.discountAmount}]]</label>
                </div>
            </div>
        </div>
        <div class="col-sm-12" >
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-6 control-label">优惠价格：</label>
                    <input class="col-sm-6" type="number" onchange="discountFun()" id="discountAmount" name="discountAmount" th:field="${statement.discountAmount}"/>
                </div>
            </div>
        </div>
    </div>

    <div class="btn-group-sm" id="statementItemTool" role="group">
        <a class="btn btn-success" onclick="saveItem()">
            <i class="fa fa-plus"></i> 保存
        </a>
        <a class="btn btn-danger" onclick="pay1()">
            <i class="fa fa-plus"></i> 确定支付
        </a>
    </div>

    <div class="col-sm-12 select-table table-striped">
        <table id="statementItemTable"></table>
    </div>

</div>
<div class="ui-layout-east">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="post-form">
                <div class="select-list">
                    <ul>
                        <li>
                            名称：<input type="text" name="name"/>
                        </li>
                        <li>
                            是否套餐：
                            <select name="carPackage" th:with="type=${@dict.getType('si_car_package')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li>
                            服务分类：
                            <select name="serviceCatalog" th:with="type=${@dict.getType('si_service_catalog')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="serviceItemTool" role="group">

        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="serviceItemTable"></table>
        </div>
    </div>
</div>
    <th:block th:include="common/include :: footer" />
    <th:block th:include="common/include :: layout-latest" />
    <script th:inline="javascript">
        // OUTER-LAYOUT
        $('body').layout({
            east__size:	'30%',
        });


        $(function() {
            var options = {
                id:"serviceItemTable",
                toolbar: "serviceItemTool",
                uniqueId:"id",
                url:"/business/serviceItem/query?saleStatus=1",
                columns: [
                    {
                        field: 'id',
                        title: '服务项id',
                        visible: false
                    },
                    {
                        field: 'name',
                        title: '服务项名称',
                        sortable: true
                    },
                    {
                        field: 'originalPrice',
                        title: '服务项价格',
                        sortable: true
                    },
                    {
                        field: 'info',
                        title: '备注信息',
                        sortable: true,
                        formatter: function(value, row, index) {
                            return $.table.tooltip(value, 0, "open");
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        formatter: function(value, row, index) {
                            var actions = [];
                                actions.push('<a class="btn btn-info btn-xs " href="javascript:void(0)" ' +
                                    'onclick="addserviceItem(\'' + row.id + '\')"><i class="fa fa-plus"></i></a> ');
                            return actions.join('');
                        }
                    }]
            };
            $.table.init(options);
        });

        $(function() {
            var statementId = [[${statement.id}]];
            var options = {
                id:"statementItemTable",
                toolbar:"statementItemTool",
                uniqueId: "itemId",
                url:"/business/statementItem/query?statementId="+statementId,
                columns: [
                    {
                        field: 'id',
                        title: '中间表id',
                        visible: false
                    },
                    {
                        field: 'itemName',
                        title: '服务项名称',
                        sortable: true
                    },
                    {
                        field: 'itemPrice',
                        title: '服务项价格',
                        sortable: true
                    },
                    {
                        field: 'itemQuantity',
                        title: '购买数量',
                        sortable: true,
                        formatter: function(value, row, index) {
                            return $.table.tooltip(value, 0, "open");
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        formatter: function(value, row, index) {
                            var actions = [];
                            actions.push('<a class="btn btn-info btn-xs " href="javascript:void(0)" ' +
                                'onclick="addserviceItem(\'' + row.itemId + '\')"><i class="fa fa-plus"></i></a> ');
                            actions.push('<a class="btn btn-warning btn-xs " href="javascript:void(0)" ' +
                                'onclick="decrServiceItem(\'' + row.itemId + '\')"><i class="fa fa-minus"></i></a> ');
                            return actions.join('');
                        }
                    }]
            };
            $.table.init(options);
        });

        function addserviceItem(id) {
            //查询出改行的数据
            var rs=$('#serviceItemTable').bootstrapTable('getRowByUniqueId',id);
            //判断该行的数据在左侧中的关系表中是否存在,如果存在则修改数量+1
            //如果不存在则追加一条数据到中间表的数据中
            var statementItemData=$('#statementItemTable').bootstrapTable('getRowByUniqueId',id);
            if (statementItemData){
                $('#statementItemTable').bootstrapTable('updateCellByUniqueId', {
                    id: id,
                    field: 'itemQuantity',
                    value: statementItemData.itemQuantity +1
                });
            }else{
                $('#statementItemTable')
                    .bootstrapTable('append', {"itemName":rs.name,"itemPrice":rs.discountPrice,"itemQuantity":1,"itemId":id});
            }
            discountFun();
        }

        function decrServiceItem(id) {
            //如果表格中没有数据,那么就将数据这是为0
            var data= $('#statementItemTable').bootstrapTable('getData',false);
            if (data.length==0){
                $("#totalAmount").html(0);
                $("#actuallyPaid").html(0);
                $("#discountAmount").val(0);
            }
            //获取该行的数据,如果数量大于1 则数量-1
            //如果数量小于或者等于1则删除数据
            var statementItemData=$('#statementItemTable').bootstrapTable('getRowByUniqueId',id);
            if (statementItemData.itemQuantity>1){
                $('#statementItemTable').bootstrapTable('updateCellByUniqueId', {
                    id: id,
                    field: 'itemQuantity',
                    value: statementItemData.itemQuantity -1
                })
            }else{
                $('#statementItemTable').bootstrapTable('removeByUniqueId',id);
            }
            discountFun();
        }

        function discountFun() {
            //获取下方表格中的所有数据
            var data= $('#statementItemTable').bootstrapTable('getData',false);
            var totalAmount = 0;
            var actuallyPaid = 0;
            //计算总金额
            $.each(data,function (index,item) {
                totalAmount += item.itemPrice * item.itemQuantity;
            });
            //判断优惠金额是否大于0   设置优惠金额为0
            var discountAmount = $("#discountAmount").val();
            if (discountAmount < 0){
                $("#actuallyPaid").html(totalAmount);
                $("#discountAmount").val(0);
                return;
            }
            //判断优惠金额是否为空     设置优惠金额为0
            if (!discountAmount){
                $("#discountAmount").val(0);
                return;
            }
            //判断优惠金额是否大于总金额 弹窗警告,设置优惠金额为0
            if (discountAmount>=totalAmount){
                $.modal.confirm("优惠金额不能大于总金额",function () {
                    $("#actuallyPaid").html(totalAmount);
                    $("#discountAmount").val(0);
                    return;
                });
            }
            //计算实付金额
            var actuallyPaid = totalAmount - discountAmount;
            //给金额标签设置值
            $("#totalAmount").html(totalAmount);
            $("#actuallyPaid").html(actuallyPaid);
        }
            function saveItem() {
            // var statementId = [[${statement.id}]];
            // var data= $('#statementItemTable').bootstrapTable('getData',false);
            // $.each(data,function (index,item) {
            //     item.statementId = statementId;
            // });
            // var discountAmount = $("#discountAmount").val();
            // data.push({"itemPrice":discountAmount,"statementId":statementId});
            // //发送json ajax请求的时候需要设置发送数据的格式 //发送ajax请求后要刷新table

            var statementId = [[${statement.id}]];
            //获取表格中的所有数据 给数据加一个statementId属性

            var data= $('#statementItemTable').bootstrapTable('getData',false);
            $.each(data,function (index,item) {
                item.statementId = statementId;
            });
            //获取优惠价格
            var discountAmount = $("#discountAmount").val();
            var item ={};
            item.discountAmount = discountAmount;
            item.list=data;

            var config = {
                url: "/business/statementItem/saveItem",
                type: "post",
                dataType: "json",
                data: JSON.stringify(item),
                contentType: 'application/json;charset=UTF-8',
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                },
                success: function(result) {
                    $.table.refresh("statementItemTable");
                    $.operate.ajaxSuccess(result);
                }
            };
            $.ajax(config)
        }

        function pay() {
            //获取到statementId
            var statementId = [[${statement.id}]];
            $.modal.confirm("确定客户已经支付吗",function () {
                $.post("/business/statementItem/pay?statementId="+statementId,function (data) {
                    if (data.code==0){
                        location.reload("/business/statementItem/itemDetail?statementId="+statementId);
                    }else{
                        $.modal.confirm(data.msg);
                    }
                });
            });
        }

        function pay1() {
            var statementId = [[${statement.id}]];
            //获取表格数据
            var data= $('#statementItemTable').bootstrapTable('getData',false);
            $.each(data,function (index,item) {
                item.statementId = statementId;
            });
            //获取优惠价格
            var discountAmount = $("#discountAmount").val();
            var item ={};
            item.discountAmount = discountAmount;
            item.list=data;
            item.statementId = statementId;
            //发送ajax请求
            var config = {
                url: "/business/statementItem/pay1",
                type: "post",
                dataType: "json",
                data: JSON.stringify(item),
                contentType: 'application/json;charset=UTF-8',
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                },
                success: function(result) {
                    if (result.code==0){
                        location.reload("/business/statementItem/itemDetail?statementId="+statementId);
                    }else{
                        $.modal.confirm(data.msg);
                    }
                }
            };
            $.ajax(config)
        };

    </script>
</body>
</html>